package com.ahm.controller;


import com.ahm.base.BaseController;

import com.ahm.service.PermissionService;
import com.ahm.service.UserService;
import com.ahm.utils.LoginUserUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController extends BaseController {

    @Resource
    private UserService userService;

    @Resource
    private PermissionService permissionService;

    @RequestMapping("index")
    public String  index(){
        return "login";
    }

    @RequestMapping("welcome")
    public String  welcome(){
        return  "welcome";
    }

    @RequestMapping("main")
    public String  main(HttpServletRequest req,Authentication authentication){
        User user = (User) authentication.getPrincipal();

        String username = user.getUsername();
//        userService

        //获取用户信息,从Cookie获取用户用户的ID
//        Integer userId = LoginUserUtil.releaseUserIdFromCookie(req);
        //根据ID查询用户信息
        com.ahm.bean.User user2 = userService.selectByUserName(username);
        //存储User
//        req.setAttribute("user",user);
        req.setAttribute("user",user2);
//        req.setAttribute();
        // 根据用户id 查询权限码 permission roleUser user_id-->roleId-->acl_value
        // select acl_value from t_permission p  left join t_user_role ur as ur on ur.role_id=p.role_id where ur.user_id = #{userId}
        List<String> aclValues =  permissionService.selectAclValueByUserId(user2.getId());
        // 保存这些权限码
        req.getSession().setAttribute("permissions",aclValues);
        //返回目标试图
        return  "main";
    }



















//@RequestMapping("main")
//    public String  main(HttpServletRequest req ){
//        //获取用户信息,从Cookie获取用户用户的ID
//        Integer userId = LoginUserUtil.releaseUserIdFromCookie(req);
//        //根据ID查询用户信息
//        User user = userService.selectByPrimaryKey(userId);
//        //存储User
//        req.setAttribute("user",user);
//        //统计用户拥有的权限码
//        List<String> permisssions=permissionService.queryUserHasRolesHasPermissions(userId);
//        //存储
//        req.getSession().setAttribute("permisssions",permisssions);
//        //返回目标试图
//        return  "main";
//    }

}
